const mongoose = require('mongoose');
const { errorModel } = require('./../models/errorModel');

// 获取数据
async function getAll(req,res){
    console.log(req.params.id)
    let result=await mongoose.model('errorModel').find({studentId:req.params.id}).populate({
        path:"exerciseId",
    }).populate({
        path:"studentId",
    })
    // console.log(result)
    if (result.length > 0) {
        res.send({
            code: 1,
            message: '获取成功',
            result:result
        })

    } else {
        res.send({
            code: 0,
            message: '获取失败',
        })
    }
}

async function del(req,res){
    let result=await mongoose.model('errorModel').deleteOne({_id:req.body.id})
    console.log(result);
    if (result.deletedCount != 0) {
        res.send({
            code: 1,
            message: '删除成功',
        })

    } else {
        res.send({
            code: 0,
            message: '删除失败',
        })
    }
}

async function add(req,res){
    console.log(req.body)
    let result=await mongoose.model('errorModel').find({...req.body});
    console.log(result.length)
    if(result.length>0){
        res.send({
            message:"添加失败",
            code:0
        })
    }else{
        await mongoose.model('errorModel').create({...req.body});
        res.send({
            message:"添加成功",
            code:1
        })
    }
}
module.exports={getAll,del,add}